home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / BTREE101.ZIP / BTOPEN.C < prev    next >
Text File  |  1990-06-20  |  4KB  |  170 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btopen.c    1.4 - 90/06/20" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. /*#include <stddef.h>*/
  9. /*#include <string.h>*/
  10.  
  11. /* local headers */
  12. #include "btree_.h"
  13.  
  14. /* btree control structure table definition */
  15. btree_t btb[BTOPEN_MAX];
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      btopen - open a btree
  20.  
  21. SYNOPSIS
  22.      btree_t *btopen(filename, type, fldc, fldv)
  23.      const char *filename;
  24.      const char *type;
  25.      int fldc;
  26.      const btfield_t fldv[];
  27.  
  28. DESCRIPTION
  29.      The btopen function opens the file named by filename as a btree.
  30.      A pointer to the btree_t control structure associated with the
  31.      file is returned.
  32.  
  33.      type is a character string having one of the following values:
  34.  
  35.           "r"            open for reading
  36.           "r+"           open for update (reading and writing)
  37.  
  38.      See btcreate for explanation of the field count fldc and the
  39.      field definition list fldv.
  40.  
  41.      btopen will fail if one or more of the following is true:
  42.  
  43.      [EINVAL]       filename is the NULL pointer.
  44.      [EINVAL]       type is not "r" or "r+".
  45.      [EINVAL]       fldc is less than 1.
  46.      [EINVAL]       fldv is the NULL pointer.
  47.      [EINVAL]       fldv contains an invalid field definition.
  48.      [ENOENT]       The named btree file does not exist.
  49.      [BTECORRUPT]   The named file is corrupt.
  50.      [BTEEOF]       No file header.
  51.      [BTEMFILE]     Too many open btrees.  The maximum is defined as
  52.                     BTOPEN_MAX in <btree.h>.
  53.  
  54. SEE ALSO
  55.      btclose, btcreate.
  56.  
  57. DIAGNOSTICS
  58.      On failure btopen returns a NULL pointer, and errno set to
  59.      indicate the error.
  60.  
  61. NOTES
  62.      The same field count and field definition list with which the
  63.      btree was created must be used each time the btree is opened.
  64.      Otherwise the results are undefined.
  65.  
  66. ------------------------------------------------------------------------------*/
  67. btree_t *btopen(filename, type, fldc, fldv)
  68. const char *filename;
  69. const char *type;
  70. int fldc;
  71. const btfield_t fldv[];
  72. {
  73.     btree_t *    btp    = NULL;
  74.     int        terrno    = 0;        /* tmp errno */
  75.  
  76.     /* validate arguments */
  77.     if (filename == NULL || type == NULL) {
  78.         errno = EINVAL;
  79.         return NULL;
  80.     }
  81.  
  82.     /* find free slot in btb table */
  83.     for (btp = btb; btp < btb + BTOPEN_MAX; ++btp) {
  84.         if (!(btp->flags & BTOPEN)) {
  85.             break;        /* found */
  86.         }
  87.     }
  88.     if (btp >= btb + BTOPEN_MAX) {
  89.         errno = BTEMFILE;    /* no free slots */
  90.         return NULL;
  91.     }
  92.  
  93.     /* open file */
  94.     if (strcmp(type, BT_READ) == 0) {
  95.         btp->flags = BTREAD;
  96.     } else if (strcmp(type, BT_RDWR) == 0) {
  97.         btp->flags = BTREAD | BTWRITE;
  98.     } else {
  99.         errno = EINVAL;
  100.         return NULL;
  101.     }
  102.     btp->bp = bopen(filename, type, sizeof(bthdr_t), (size_t)1, (size_t)0);
  103.     if (btp->bp == NULL) {
  104.         if ((errno != ENOENT) && (errno != EACCES)) BTEPRINT;
  105.         terrno = errno;
  106.         memset(btp, 0, sizeof(*btb));
  107.         btp->flags = 0;
  108.         errno = terrno;
  109.         return NULL;
  110.     }
  111.  
  112.     /* load btree_t structure */
  113.     if (bgeth(btp->bp, &btp->bthdr) == -1) {    /* header */
  114.         BTEPRINT;
  115.         if (errno == BEEOF) errno = BTEEOF;
  116.         else if (errno == BEBOUND) errno = BTECORRUPT;
  117.         terrno = errno;
  118.         bclose(btp->bp);
  119.         memset(btp, 0, sizeof(*btb));
  120.         btp->flags = 0;
  121.         errno = terrno;
  122.         return NULL;
  123.     }
  124.     if (btp->bthdr.flags & BTHMOD) {        /* corrupt file */
  125.         bclose(btp->bp);
  126.         memset(btp, 0, sizeof(*btb));
  127.         btp->flags = 0;
  128.         errno = BTECORRUPT;
  129.         return NULL;
  130.     }
  131.     if (!bt_fvalid(btp->bthdr.keysize, fldc, fldv)) {
  132.         bclose(btp->bp);
  133.         memset(btp, 0, sizeof(*btb));
  134.         btp->flags = 0;
  135.         errno = EINVAL;
  136.         return NULL;
  137.     }
  138.     btp->fldc = fldc;        /* field count */
  139.     btp->fldv = NULL;
  140.     btp->cbtpos.node = NIL;        /* cursor */
  141.     btp->cbtpos.key = 0;
  142.     btp->cbtnp = NULL;
  143.     btp->sp = NULL;
  144.     if (bt_alloc(btp) == -1) {    /* allocate memory for btree */
  145.         BTEPRINT;
  146.         terrno = errno;
  147.         bclose(btp->bp);
  148.         memset(btp, 0, sizeof(*btb));
  149.         btp->flags = 0;
  150.         errno = terrno;
  151.         return NULL;
  152.     }
  153.     memcpy(btp->fldv, fldv, btp->fldc * sizeof(*btp->fldv));
  154.  
  155.     /* set up buffering */
  156.     if (bsetvbuf(btp->bp, NULL, bt_blksize(btp), BTBUFCNT) == -1) {
  157.         BTEPRINT;
  158.         terrno = errno;
  159.         bt_free(btp);
  160.         bclose(btp->bp);
  161.         memset(btp, 0, sizeof(*btb));
  162.         btp->flags = 0;
  163.         errno = terrno;
  164.         return NULL;
  165.     }
  166.  
  167.     errno = 0;
  168.     return btp;
  169. }
  170.